package com.obao.user.service.impl;

import com.obao.business.dao.IBusinessDao;
import com.obao.business.dao.IProductDao;
import com.obao.business.entity.Business;
import com.obao.business.entity.Product;
import com.obao.business.entity.ProductFlavor;
import com.obao.business.entity.ProductSize;
import com.obao.user.dao.ICartDao;
import com.obao.user.entity.BusinessItem;
import com.obao.user.entity.Cart;
import com.obao.user.entity.ProductItem;
import com.obao.user.entity.User;
import com.obao.user.service.ICartService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;


public class CartService implements ICartService {
    private ICartDao cartDao;
    private IProductDao productDao;
    private IBusinessDao businessDao;

    /**
     * * 业务层
     * 添加购物车
     *
     * @param user
     * @param buyNumber
     * @param product
     * @param productFlavor
     * @param productSize
     */
    @Override
    public void save(User user, int buyNumber, Product product, ProductFlavor productFlavor, ProductSize productSize) {
        try {
            StringBuffer opStr = new StringBuffer();
            System.out.println("userID=" + user.getOpenid() + " buyNumber:" + buyNumber + " productrID:" + product.getProductId() + " sizeId:" + productSize.getId() + " flavorId:" + productFlavor.getFlavorId());
            Business business = new Business();
            // 查询出产品所在商家信息(商家ID)
            Integer businessId = businessDao.findBusinessIdByProduct(product);
            business.setBusinessId(businessId);
            Cart cart = cartDao.findCart(user);
            if (cart == null) {
                //用户没有购物车
                cart = new Cart();
                opStr.append("user dont't cart....");
                cart.setUser(user);
                cart.getBusinessItems().add(warpBusinessItem(business, buyNumber, product, productFlavor, productSize));
                cartDao.save(cart);
            } else {
                //用户有购物车
                opStr.append("user has cart...");
                //判断是否存在改产品所属商家项
                BusinessItem businessItem = null;
                for (BusinessItem item : cart.getBusinessItems()) {
                    if (item.getBusiness().getBusinessId().equals(businessId)) {
                        opStr.append("has businessItem...");
                        businessItem = item;
                        break;
                    }
                }
                if (businessItem == null) {
                    //没有此商家项
                    opStr.append("dont't has businessItem...");
                    businessItem = warpBusinessItem(business, buyNumber, product, productFlavor, productSize);
                    businessItem.setCart(cart);
                    cartDao.save(businessItem);
                } else {
                    ProductItem productItem = null;
                    for (ProductItem item : businessItem.getProductItems()) {
                        if (item.getProduct().getProductId().equals(product.getProductId()) &&
                                item.getProductFlavor().getFlavorId().equals(productFlavor.getFlavorId()) &&
                                item.getProductSize().getId().equals(productSize.getId())) {
                            //判断是否存在相同的产品项(口味相同,尺寸相同,同一产品)
                            opStr.append("has productItem...");
                            productItem = item;
                            break;
                        }
                    }
                    if (productItem == null) {
                        opStr.append("dont' has productItem....");
                        productItem = warpProductItem(buyNumber, product, productFlavor, productSize);
                        productItem.setBusinessItem(businessItem);
                        cartDao.save(productItem);
                    } else {
                        //修改对应产品项的数量
                        cartDao.updateProductItem(buyNumber, productItem.getProductItemId());
                    }
                }

            }
            System.out.println(opStr.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 包装产品项
     *
     * @param buyNumber
     * @param product
     * @param productFlavor
     * @param productSize
     * @return
     */
    public ProductItem warpProductItem(int buyNumber, Product product, ProductFlavor productFlavor, ProductSize productSize) {
        ProductItem productItem = new ProductItem();
        productItem.setProductNumber(buyNumber);
        productItem.setProduct(product);
        productItem.setProductFlavor(productFlavor);
        productSize = productDao.findBySizeId(productSize.getId());
        productItem.setProductSize(productSize);
        return productItem;

    }

    /**
     * 包装商家项
     *
     * @return
     */
    public BusinessItem warpBusinessItem(Business business, int buyNumber, Product product, ProductFlavor productFlavor, ProductSize productSize) {
        BusinessItem businessItem = new BusinessItem();
        ProductItem productItem = warpProductItem(buyNumber, product, productFlavor, productSize);
        businessItem.setBusiness(business);
        businessItem.getProductItems().add(productItem);
        return businessItem;
    }

    @Override
    public Cart findCart(User user) {
        return cartDao.findCart(user);

    }

    @Override
    public void delete(Integer productItemId) {
        //级联获取商品项信息
        ProductItem productItemInfo = cartDao.findProductItem(productItemId);
        //直接删除商品项
        cartDao.deleteProductItem(productItemId);
        //删完此商品项后判断其商家项下是否有商品项

        Integer businessItemId = productItemInfo.getBusinessItem().getBusinessItemId();
        //如果该商家项下不存在商品项就删除该商家项
        if (!cartDao.checkBusinessItemIsExistProductItem(businessItemId)) {
            cartDao.deleteBusinessItem(businessItemId);
            cartDao.checkCartIsExistBusinessItem(businessItemId);
        }

    }

    @Override
    public List<BusinessItem> findBuyingBill(String businessItemIds, String productItemIds) {
        String[] businessItemIdsArr = businessItemIds.split("MD5");
        String[] productItemIdsArr = productItemIds.split("MD5");
        List<BusinessItem> bs = Collections.synchronizedList(new ArrayList<BusinessItem>());
        List<BusinessItem> returnBusinessItems = Collections.synchronizedList(new ArrayList<BusinessItem>());
        for (String item : businessItemIdsArr) {
            if (item != "") {
                bs.add(cartDao.findBusinessItemById(Integer.parseInt(item)));
            }
        }
       //遍历全部商家项
        for (BusinessItem btm : bs) {
            //遍历单个商家项下全部商品项
            BusinessItem br = new BusinessItem();
            br.setCart(btm.getCart());
            br.setBusiness(btm.getBusiness());
            for (ProductItem ptm : btm.getProductItems()) {
                    //筛选出产品项中含有选中的商品项
                for(String ptmId:productItemIdsArr){
                    //遍历所有选中的产品项ID   与每个商家项下的全部产品项ID对比
                    if(ptm.getProductItemId()==(Integer.parseInt(ptmId))){
                        br.getProductItems().add(ptm);
                    }
                }
            }
            if(br.getProductItems().size()>0){
                returnBusinessItems.add(br);
            }
        }
        return returnBusinessItems;
    }


    public void setCartDao(ICartDao cartDao) {
        this.cartDao = cartDao;
    }

    public void setProductDao(IProductDao productDao) {
        this.productDao = productDao;
    }

    public void setBusinessDao(IBusinessDao businessDao) {
        this.businessDao = businessDao;
    }
}
